package com.yic.module.mes.api.moroute;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.module.mes.api.moroute.dto.MoRouteRespDTO;
import com.yic.module.mes.convert.moroute.MoRouteConvert;
import com.yic.module.mes.dal.dataobject.moroute.MoRouteDO;
import com.yic.module.mes.dal.dataobject.process.ProcessDO;
import com.yic.module.mes.service.moroute.MoRouteService;
import com.yic.module.mes.service.process.ProcessService;
import org.mapstruct.ap.shaded.freemarker.cache.FileTemplateLoader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工单工艺路线API实现
 */
@Service
public class MoRouteApiImpl implements MoRouteApi {

    @Resource
    private MoRouteService moRouteService;
    @Resource
    private ProcessService processService;

    @Override
    public List<MoRouteRespDTO> getMoRouteList(Long id) {
        List<MoRouteDO> list = moRouteService.getMoRouteByMoId(id);
        List<MoRouteRespDTO> resp = MoRouteConvert.INSTANCE.convertList04(list);
        if (CollectionUtil.isNotEmpty(resp)) {
            resp.forEach(item -> {
                ProcessDO process = processService.getProcess(item.getProcessId());
                if (ObjectUtil.isNotEmpty(process)) {
                    item.setProcessCode(process.getCode());
                    item.setProcessName(process.getName());
                }
            });
        }
        return resp;
    }
}
